﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exentials.Xoox.Spreadsheet
{
    public static class FormulaExtensions
    {
        public static Formula IF(this Formula expression1, Formula expression, Formula ifTrue)
        {
            return new Formula("IF({0},{1})", expression, ifTrue);
        }

        public static Formula IF(this Formula expression1, Formula expression, Formula ifTrue, Formula ifFalse)
        {
            return new Formula("IF({0},{1},{2})", expression, ifTrue, ifFalse);
        }

        public static Formula Add(this Formula expression1, Formula expression)
        {
            return new Formula("({0}+{1})", expression1, expression);
        }

        public static Formula Sub(this Formula expression1, Formula expression)
        {
            return new Formula("({0}-{1})", expression1, expression);
        }

        public static Formula Sum(this Formula expression1)
        {
            return new Formula("SUM({0})", expression1);
        }

        public static Formula Avg(this Formula expression1, params Formula[] expressions)
        {
            return new Formula("AVG({0})", expressions);
        }

        public static Formula Value(this Formula expression1, int number)
        {
            return Value(expression1, (object)number);
        }

        public static Formula Value(this Formula expression1, decimal number)
        {
            return Value(expression1, (object)number);
        }

        public static Formula Value(this Formula expression1, string text)
        {
            return Value(expression1, (object)text);
        }

        public static Formula Value(this Formula expression1, bool truefalse)
        {
            return Value(expression1, (object)truefalse);
        }

        internal static Formula Value(this Formula expression1, object obj)
        {
            if (!expression1.IsNull)
            {
                Formula.FormulaCollection formulas = new Formula.FormulaCollection();
                formulas.Add(expression1);
                formulas.Add(new Formula(obj));
                return new Formula("{0}", formulas);
            }
            else return new Formula("{0}", obj);
        }

        public static Formula Ref(this Formula expression1, Reference reference)
        {
            if (!expression1.IsNull)
            {
                Formula.FormulaCollection formulas = new Formula.FormulaCollection();
                formulas.Add(expression1);
                formulas.Add(new Formula(reference));
                return new Formula("{0}", formulas);
            }
            else return new Formula("{0}", reference);
        }
    }
}
